package com.at.async1;

import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/AsyncLongRunningServlet", asyncSupported = true)
public class AsyncLongRunningServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        final String id = request.getParameter("id");
        System.out.println("AsyncLongRunningServlet Start::ID=" + id);
        request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
        String time = request.getParameter("time");
        int secs = Integer.valueOf(time);
        // max 10 seconds
        if (secs > 10000) {
            secs = 10000;
        }
        AsyncContext asyncCtx = request.startAsync();
        asyncCtx.addListener(new AppAsyncListener(id));
        asyncCtx.setTimeout(9000);
        ThreadPoolExecutor executor = (ThreadPoolExecutor) request.getServletContext().getAttribute("executor");
        executor.execute(new AsyncRequestProcessor(asyncCtx, secs, id));
        long endTime = System.currentTimeMillis();
        System.out.println("AsyncLongRunningServlet End::ID="+id + "::Time Taken="+ (endTime - startTime) + " ms.");
    }

}
